package com.ruigi.control.advice;

import com.ruigi.Exception.Customexception;
import com.ruigi.pojo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j

public class Projectexceptionadvice {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> doexception(SQLIntegrityConstraintViolationException ex) {
        log.error(ex.getMessage());
//
        if (ex.getMessage().contains("Duplicate entry")) {
            String[] splits = ex.getMessage().split(" ");
            String msg = splits[2] + "已存在";
            return R.error(msg);
        }

        System.out.println("123");
        return R.error("ggg失败了");
    }

    @ExceptionHandler(Customexception.class)
    public R<String> custumception(Customexception ex) {

        return R.error(ex.getMessage());

    }
}
